﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

namespace AutoMouseClick
{
    public struct Pos_Color
    {
        public Point pos;
        public Color clr;
        public int d, c; //dong va cot, col and row
    }

    public struct SimColor
    {
        public ArrayList arrSimCell;
    }

    public partial class PlayImageRelation : Form
    {
        private RunClick rClick = new RunClick();
        private Pos_Color pos1 = new Pos_Color();
        private Pos_Color pos2 = new Pos_Color();
        private Pos_Color posBegin = new Pos_Color();
        private ArrayList arrSimColor = new ArrayList();
        private Hashtable ht = new Hashtable();
        private Thread runPuzzle;

        public PlayImageRelation()
        {
            InitializeComponent();
        }

        private void btn_Choose2Milestone_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            Thread.Sleep(int.Parse(txtB_WaitTime.Text));
            pos1.pos = Cursor.Position;
            pos1.clr = rClick.GetColorAtMouseClick();
            txtB_Milestone1.Text = pos1.pos.X.ToString() + "," + pos1.pos.Y.ToString() + "-" + pos1.clr.A.ToString() + "," + pos1.clr.R.ToString() + "," + pos1.clr.G.ToString() + "," + pos1.clr.B.ToString();
            this.WindowState = FormWindowState.Normal;

        }

        private void btn_Milestone2_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            Thread.Sleep(int.Parse(txtB_WaitTime.Text));
            pos2.pos = Cursor.Position;
            //pos2.pos.Y = pos1.pos.Y;
            //RunClick.SendClick(pos2.pos);
            //Thread.Sleep(2000);
            pos2.clr = rClick.GetColorAtMouseClick();
            txtB_Milestone2.Text = pos2.pos.X.ToString() + "," + pos2.pos.Y.ToString() + "-" + pos2.clr.A.ToString() + "," + pos2.clr.R.ToString() + "," + pos2.clr.G.ToString() + "," + pos2.clr.B.ToString();
            this.WindowState = FormWindowState.Normal;
        }

        private void btn_CalCellWidth_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            Thread.Sleep(int.Parse(txtB_WaitTime.Text));
            int n = pos2.pos.X - pos1.pos.X;
            Pos_Color[] arrPosClr = new Pos_Color[n];
            for (int i = 0; i < n; i++)
            {
                arrPosClr[i].pos.X = pos1.pos.X + i;
                arrPosClr[i].pos.Y = pos1.pos.Y;
                RunClick.SendClick(arrPosClr[i].pos);
                arrPosClr[i].clr = rClick.GetColorAtMouseClick();
            }

            //matching
            int beginMatchi = -1;
            int beginMatchj = -1;
            int countMatch = 0;
            int maxMatch = 0;
            int beginMaxMatchi = -1;
            int beginMaxMatchj = -1;

            for (int i = 0; i < arrPosClr.Length-1; i++) 
            {
                for (int j = i+1; j < arrPosClr.Length; j++)
                {
                    if (arrPosClr[i].clr == arrPosClr[j].clr)
                    {
                        beginMatchi = i;
                        beginMatchj = j;
                        countMatch = 1;
                        for (int k = 1; (k < arrPosClr.Length - i) && (k < arrPosClr.Length - j); k++)
                        {
                            if (arrPosClr[i + k].clr == arrPosClr[j + k].clr)
                            {
                                countMatch++;
                            }
                            else break;
                        }
                        if (countMatch > maxMatch)
                        {
                            beginMaxMatchi = beginMatchi;
                            beginMaxMatchj = beginMatchj;
                            maxMatch = countMatch;
                        }
                        countMatch = 0;
                    }                    
                }
            }

            if (maxMatch != 0)
            {
                int intervalCell = (arrPosClr[beginMaxMatchj].pos.X - arrPosClr[beginMaxMatchi].pos.X) / int.Parse(txtB_NumberCell.Text);
                txtB_CellWidth.Text = intervalCell.ToString();
            }

            this.WindowState = FormWindowState.Normal;
        }

        private void btnListColor_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            Thread.Sleep(int.Parse(txtB_WaitTime.Text));
            int n = pos2.pos.X - pos1.pos.X;
            int m = pos2.pos.Y - pos1.pos.Y;
            Pos_Color[] arrPosClr = new Pos_Color[n];
            for (int j = 0; j < m; j++)
            {
                for (int i = 0; i < n; i++)
                {
                    arrPosClr[i].pos.X = pos1.pos.X + i;
                    arrPosClr[i].pos.Y = pos1.pos.Y + j;
                    //RunClick.SendClick(arrPosClr[i].pos);
                    arrPosClr[i].clr = rClick.GetColorAtMouseClick(arrPosClr[i].pos.X, arrPosClr[i].pos.Y);
                    rTextB_Result.AppendText(arrPosClr[i].clr.A.ToString() + "," + arrPosClr[i].clr.R.ToString() + "," +
                                                arrPosClr[i].clr.G.ToString() + "," + arrPosClr[i].clr.B.ToString() + "\n");
                }
            }
            this.WindowState = FormWindowState.Normal;
        }

        private void btn_ChooseBeginPos_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            Thread.Sleep(4000);
            posBegin.pos = Cursor.Position;
            posBegin.clr = rClick.GetColorAtMouseClick();
            txtB_BeginPos.Text = posBegin.pos.X.ToString() + "," + posBegin.pos.Y.ToString() + "-" + posBegin.clr.A.ToString() + "," + posBegin.clr.R.ToString() + "," + posBegin.clr.G.ToString() + "," + posBegin.clr.B.ToString();
            this.WindowState = FormWindowState.Normal;
        }

        private void btn_RunGetPair_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            rClick.RunState = true;
            runPuzzle = new Thread(new ThreadStart(ClickPair));
            runPuzzle.Start();
        }

        private void ClickPair()
        {
            Thread.Sleep(4000);
            int intervalCell = int.Parse(txtB_CellWidth.Text);
            int x, y;
            Color clr;
            int delta = 0;
            while (rClick.RunState)
            {
                ht.Clear();
                arrSimColor.Clear();
                rClick.RefreshImage = true;
                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 14; j++)
                    {
                        x = posBegin.pos.X + delta + j * intervalCell;
                        y = posBegin.pos.Y + delta + i * intervalCell;

                        clr = rClick.GetColorAtMouseClick(x, y);
                        Pos_Color pc = new Pos_Color();
                        pc.clr = clr;
                        pc.pos = new Point(x, y);
                        pc.d = i;
                        pc.c = j;

                        if (!ht.ContainsKey(clr))
                        {
                            ht.Add(clr, arrSimColor.Count);

                            SimColor sc = new SimColor();
                            sc.arrSimCell = new ArrayList();
                            sc.arrSimCell.Add(pc);
                            arrSimColor.Add(sc);
                        }
                        else
                        {
                            int idx = (int)ht[clr];
                            SimColor sc = (SimColor)arrSimColor[idx];
                            sc.arrSimCell.Add(pc);
                            arrSimColor.RemoveAt(idx);
                            arrSimColor.Insert(idx, sc);
                        }
                    }
                }

                if (delta < intervalCell / 2) delta += 2;
                else delta = 0;
                //foreach (SimColor sc in arrSimColor)
                //{
                //    foreach (Pos_Color pc in sc.arrSimCell)
                //    {
                //        rTextB_Result.AppendText(pc.d + "," + pc.c + "  ");
                //    }
                //    rTextB_Result.AppendText("\n");
                //}

                while (rClick.RefreshImage)
                {
                    foreach (SimColor sc in arrSimColor)
                    {
                        for (int i = 0; i < sc.arrSimCell.Count - 1; i++)
                            for (int j = i + 1; j < sc.arrSimCell.Count; j++)
                            {
                                if (!rClick.RunState)
                                {
                                    //this.WindowState = FormWindowState.Normal;
                                    return;
                                }

                                Pos_Color pc1 = (Pos_Color)sc.arrSimCell[i];
                                Pos_Color pc2 = (Pos_Color)sc.arrSimCell[j];
                                RunClick.SendClick(pc1.pos);
                                Thread.Sleep(4);
                                RunClick.SendClick(pc2.pos);
                                Thread.Sleep(4);
                            }
                    }
                }
            }
            //this.WindowState = FormWindowState.Normal;
        }

        
       
    }
}
